package cn.agiledata.bank.linkbank.impl.ceb;

import java.math.BigDecimal;
import java.util.Date;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.impl.AbstractBankNetPayRequest;
import cn.agiledata.bank.linkbank.impl.AbstractBankPayRequest;
import cn.agiledata.bank.linkbank.logic.DigitalSignGeneratable;

/**
 * 光大银行网上银行b2b支付请求
 * 
 * @author 马贤波
 * 
 */
public final class CebB2bNetPayRequest extends AbstractBankNetPayRequest
		implements DigitalSignGeneratable {
	
	public CebB2bNetPayRequest() {
        super();
        init();
    }

	protected void init() {
		setParameter(CebNetPayConstant.TRANS_NAME,
				CebNetPayConstant.TRANS_TYPE_B2B); // 交易类型:B2C
		setParameter(CebNetPayConstant.TRANS_TYPE,
				CebNetPayConstant.TRANS_TYPE_B2B); // 交易类型:B2C
		setParameter(CebNetPayConstant.CURRENCY_TYPE, CebNetPayConstant.RMB);// 货币类型：人民币
	}

	protected final AbstractBankPayRequest getCopiedInstance() {
		CebB2cNetPayRequest request = new CebB2cNetPayRequest();
		return request;
	}

	public void setMerchantId(String merchantId) {
		setParameter(CebNetPayConstant.MERCHANT_ID, merchantId);
	}

	public String getMerchantUrl() {
		return getParameter(CebNetPayConstant.MERCHANT_URL);
	}

	public void setMerchantUrl(String merchantUrl) {
		setParameter(CebNetPayConstant.MERCHANT_URL, merchantUrl);
		setParameter(CebNetPayConstant.MERCHANT_URL1, merchantUrl);
	}

	public void setOrderId(String orderId) {
		setParameter(CebNetPayConstant.ORDER_ID, orderId);
	}

	public void setAmount(BigDecimal amount) {
		setParameter(CebNetPayConstant.AMOUNT, amount.toString());
	}

	public void setMerchantName(String merchantName) {// 商户名称
		this.setParameter(CebNetPayConstant.CUST_NAME, merchantName);
	}

	public void setMerchantEmail(String merchantEmail) {// 商户Email
		this.setParameter(CebNetPayConstant.CUST_EMAIL, merchantEmail);
	}

	public void setDate(Date date) {
		String dateValue = DateUtil.date2String(date,
				CebNetPayConstant.DATE_FORMAT);
		setParameter(CebNetPayConstant.DATE, dateValue);
	}

	public void setRemark(String remark) {
		setParameter(CebNetPayConstant.REMARK, remark);
	}

	public void setDigitalSign(String digitalSign) {
		setParameter(CebNetPayConstant.SIGNATURE, digitalSign);
	}

	public void setPlain(String plain) {
		setParameter(CebNetPayConstant.PLAIN, plain);
	}

	public String generateDigitalSign(String cryptoKey)
			throws LinkBankException {
		String[] keys = { CebNetPayConstant.TRANS_TYPE,
				CebNetPayConstant.MERCHANT_ID, CebNetPayConstant.ORDER_ID,
				CebNetPayConstant.AMOUNT, CebNetPayConstant.DATE,
				CebNetPayConstant.CURRENCY_TYPE, CebNetPayConstant.CUST_NAME,
				CebNetPayConstant.PRODUCT_INFO, CebNetPayConstant.CUST_EMAIL,
				CebNetPayConstant.MERCHANT_URL,
				CebNetPayConstant.MERCHANT_URL1, CebNetPayConstant.REMARK };
		String digitalSign = CebNetPayUtils.getDigitalSign(getParameters(),
				keys);
		setDigitalSign(digitalSign);
		setPlain(CebNetPayUtils.map2String(getParameters(), keys));
		return digitalSign;
	}

	public String getCryptoKeyDescription() {
		return null;
	}

	public boolean validateCryptoKey(String cryptoKey) {
		// 不需要密钥，返回true
		return true;
	}
}
